// pages/resume/order/order.js
import {
  $wuxSelect,
  $wuxCalendar,
  $wuxToast
} from '../../../wux-components/index'
import {
  searchPublishPosition
} from '../../../utils/position.js'
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    position:'',
    positionId:'',
    isOpen:false,
    orderTime: '请选择',
    origin: 'pos', // 从个页面进
    address: '',
    peo: '',
    phone: '13900000000',
    resumeId:'',
    candidateId:'',
    type:'',
    recommendClassifyId: 0,
    justLook:true,
    resumeDeliverId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.recommendClassifyId = options.recommendClassifyId;
    this.data.resumeId = options.resumeId;
    this.data.candidateId = options.id;//应聘者id
    this.data.type = options.type;
    this.data.resumeDeliverId = options.resumeDeliverId || 0;//简历投递编号 
    this.data.recommend = options.recommendClassifyId > 0; // 是否通过扫推荐码进到此简历详情页面并进行预约的
    this.data.origin = options.origin || 'pos'; // pos || hourly
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    token = getApp().globalData.token;
  },
  onInputPosition(e) {
    this.setData({
      position: e.detail.value
    });
    if (!e.detail.value) return;
    let type = this.data.origin == 'pos' ? 0 : 1;
    searchPublishPosition(e.detail.value, type).then(res => {
      this.setData({ companyList: res, isOpen: true })
    })
  },
  onInputAddress(e) {
    this.data.address = e.detail.value;
  },
  onInputPeo(e) {
    this.data.peo = e.detail.value;
  },
  onInputPhone(e) {
    this.data.phone = e.detail.value;
  },

  onClickPosition() {
    // todo 获取所有可以预约的职位名　　ajax
    $wuxSelect('#wux-select-position').open({
      value: '',
      options: [{
        title: 'web',
        value: 0
      }],
      onConfirm: (value, index, options) => {
        if (index !== -1) {
          this.setData({
            value: value,
            position: options[index],
          })
        }
      }
    });
  },
  onSelectPosition(e) {
    let name = e.currentTarget.dataset.name;
    let positionId = e.currentTarget.dataset.id;

    this.setData({ position: name, isOpen: false, positionId: positionId});
  },
  onClickOrderTime() {
    $wuxSelect('#wux-calendar').open({
      value: [],
      multiple: false,
      onChange: (values, displayValues) => {
        this.setData({
          orderTime: displayValues
        });
        console.log('onChange', values, displayValues);
      }
    })
  },

  toast(txt) {
    T.toast({ id: 'toast', text: txt });
  },
 

  onOrder() {
    let obj = {};
    obj.positionId = this.data.positionId;
    obj.interviewAddress = this.data.address;
    obj.linkman = this.data.peo;
    obj.mobile = this.data.phone;
    obj.resumeId = this.data.resumeId;
    obj.type=this.data.type;
    obj.candidateId = Number(this.data.candidateId);
    let isError = false;
    for (const key in obj) {
      if (obj[key] == '请选择' || !obj[key]) {
        isError = true;
      }
    }
    obj.interviewTime = this.data.orderTime;
    console.log(obj);
    if(obj.interviewTime.length == 0){
      isError = true;
    }
    if (isError) {
      T.toast({ id: '#toast', text: '还有选项没有填写或者选择,请检查' });
      return;
    }

    if (!T.regExp.tel.test(obj.mobile)) {
      T.toast({ id: '#toast', text: '联系电话格式不正确, 请检查'});

      return 
    }

    console.log(obj);
    obj.interviewTime = obj.interviewTime[0];
    obj.type = Number(obj.type);
    obj.resumeId = Number(obj.resumeId);
    obj.recommend = this.data.recommend;
  
    
     // 是通过推荐扫码进行预约面试
    if (obj.recommendClassifyId>0) {
      obj.recommendClassifyId = this.data.recommendClassifyId;
    }else{
      obj.resumeDeliverId = this.data.resumeDeliverId;
    }

    // 预约成功后返回上一级
    this.postOrder(obj)
  },

  postOrder(obj) {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'interview?token=' + token,
      method: 'POST',
      data: {
        ...obj
      },
      success: (res) => {
        T.toast({ id: '#toast', text: '操作成功', success: () => {
          var pages = getCurrentPages();
          if (pages.length > 1) {
            //上一个页面实例对象
            var prePage = pages[pages.length - 2];
            // 动态改变上一级页面的数据
            prePage.setData({
              status: 2
            });
            wx.navigateBack({
              delta: 1
            })
          }
        }});
      }
    })
  },
})
